package com.lanou.service.impl;

import com.lanou.bean.LayuiJson;
import com.lanou.bean.Permission;
import com.lanou.bean.Role;
import com.lanou.bean.RolepmsExample;
import com.lanou.mapper.RoleMapper;
import com.lanou.mapper.RolepmsMapper;
import com.lanou.service.PermissionService;
import com.lanou.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RolepmsMapper rolepmsMapper;
    @Autowired
    private PermissionService permissionService;
    @Override
    public LayuiJson<Role> findAllRole(Integer pageSize, Integer currentPage) {
        List<Role> roles = roleMapper.selectByExample(null);
        //modelAndView.addObject("roleList",roles);
        //modelAndView.setViewName("admin-role");
        int startIdx = (currentPage - 1) * pageSize;
        int endIdx = currentPage * pageSize;
        if(endIdx > roles.size()) {
            endIdx = roles.size();
        }
        List<Role> roles1 = roles.subList(startIdx, endIdx);
        LayuiJson<Role> roleLayuiJson = new LayuiJson<>();
        roleLayuiJson.setCode(0);
        roleLayuiJson.setCount((long) roles.size());
        roleLayuiJson.setMsg("查询成功");
        roleLayuiJson.setData(roles1);
        return roleLayuiJson;
        //return roleMapper.selectByExample(null);
    }

    @Override
    public void delteRoleById(Integer id) {
        RolepmsExample rolepmsExample = new RolepmsExample();
        rolepmsExample.createCriteria().andRIdEqualTo(id);
        rolepmsMapper.deleteByExample(rolepmsExample);
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Map<Permission, Map<Permission, List<Permission>>> sortPermission() {
        return fenlei();
    }


    /*
    将权力按照子父节点分类
     */
    public Map<Permission, Map<Permission,List<Permission>>> fenlei(){
        Map<Permission, Map<Permission,List<Permission>>> map = new HashMap<>();
        Map<Permission, List<Permission>> map2 = new HashMap<>();
        List<Permission> allP = permissionService.findAllP();
        ArrayList<Permission> permission1 = new ArrayList<>();
        ArrayList<Permission> permission2 = new ArrayList<>();
        Permission per = null;
        for (Permission permission : allP) {
            if (permission.getpFather() == 1){
                permission1.add(permission);
            }
            if (permission.getpFather() == 2 || permission.getpFather() == 3 || permission.getpFather() == 4){
                permission2.add(permission);
            }
            if (permission.getpFather() == 0){
                per =permission;
            }
        }

        for (Permission permission:permission1){
            if (permission.getpId() == 2){
                ArrayList<Permission> permissions = new ArrayList<>();
                for (Permission permission3:permission2){
                    if (permission3.getpFather() == 2){
                        permissions.add(permission3);

                    }
                }
                map2.put(permission,permissions);
            }

            if (permission.getpId() == 3){
                ArrayList<Permission> permissions = new ArrayList<>();
                for (Permission permission3:permission2){
                    if (permission3.getpFather() == 3){
                        permissions.add(permission3);

                    }
                }
                map2.put(permission,permissions);
            }

            if (permission.getpId() == 4){
                ArrayList<Permission> permissions = new ArrayList<>();
                for (Permission permission3:permission2){
                    if (permission3.getpFather() == 4){
                        permissions.add(permission3);

                    }
                }
                map2.put(permission,permissions);
            }
        }
        map.put(per,map2);
        return map;


    }
}
